/**
 * Created with JetBrains WebStorm.
 * User: rendong
 * Date: 12-8-30
 * Time: 下午9:36
 * :$
 */


function gSprite(){}

/**
 *
 * @type {gStage}
 */
gSprite.prototype.stage = null;

gSprite.prototype.x = 0;
gSprite.prototype.y = 0;
gSprite.prototype.width = 0;
gSprite.prototype.height = 0;
gSprite.prototype.visible = true;

gSprite.prototype.totalframe =0;
gSprite.prototype.currentframe =0;

gSprite.prototype.numChild = 0;
gSprite.prototype.childs = [];



/**
 * 脏矩形区域，需要重绘的区域
 * 内部主动更新该字段
 * @type {Array} x,y, width, height
 */
gSprite.prototype.dirtyRect = [0,0,0,0];

/**
 * 渲染方法
 * @param diryRect
 */
gSprite.prototype.render = function(diryRect)
{
    if (this.bgImage != null && this.bgImage.finish == true){
       this.stage.context.drawImage(this.bgImage,0,0);
    }

    if (this.currentframe >= this.totalframe){
        this.currentframe = 0;
    }

    var img = this.spriteImages[this.currentframe];
    if (img.finish == true){
        this.stage.context.drawImage(this.spriteImages[this.currentframe],this.x,this.y);
    }
    this.currentframe++;
}

/**
 * 静态图片
 * @type {Array}
 */
gSprite.prototype.bgImage = null;

gSprite.prototype.setBgImage = function(url)
{
    /**
     *
     * @type {Image}
     */
    var img = new Image();
    img.src = url;
    this.bgImage = img;
    ImageFinishCheck(img);
}

gSprite.prototype.spriteImages = [];

gSprite.prototype.setSpriteImage = function(urlpre, ext, start, end)
{
    for(var i = start; i<= end; i++){
        var img = new Image();
        img.src = urlpre + i + ext;

        this.spriteImages.push(img);
        ImageFinishCheck(img);
    }

    this.totalframe = this.spriteImages.length;
}